package rcs34.android.photopublishare;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.Map.Entry;

import rcs34.android.libs.ExifDriver.ExifDriver;

import Utils.ImageUtils;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityPreview extends Activity {
	private final String LOGTAG = getClass().getName();
	private final String IMAGE = "image/";
	private final int REQUEST_PROFILE = 33;
	private ProgressDialog progressDialog;
	private ListView exifInfo;
	private File file = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		progressDialog = ProgressDialog.show(this, "Loading", "");
		setContentView(R.layout.activity_preview);
		Intent intent = getIntent();
		exifInfo = (ListView) findViewById(R.id.exifInfo);
		exifInfo.setAdapter(new AdapterExif(this));
		if (intent.getAction().equals(Intent.ACTION_SEND)) {
			if (intent.getType().startsWith(IMAGE)) {
				ImageDisplayer displayer = new ImageDisplayer();
				GApp.imageUri = (Uri) intent.getExtras().get(
						Intent.EXTRA_STREAM);
				displayer.execute(null, GApp.imageUri);
			}
		} else if (intent.getAction().equals(GApp.ACTION_SEND_CAPTURED_IMAGE)) {
			if (intent.getType().startsWith(IMAGE)) {
				ImageDisplayer displayer = new ImageDisplayer();
				GApp.imageUri = (Uri) intent.getExtras().get(
						Intent.EXTRA_STREAM);
				displayer.execute(GApp.imageUri, null);
			}
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent _intent) {
		if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_PROFILE) {
			progressDialog = ProgressDialog.show(this, "Reloading", "");
			ImageDisplayer displayer = new ImageDisplayer();
			displayer.execute(Uri.fromFile(file), null);
		}
	}

	private class ImageDisplayer extends
			AsyncTask<Uri, Drawable, ArrayList<String[]>> {
		@Override
		protected ArrayList<String[]> doInBackground(Uri... _path) {
			// _path[0] holds path to file, _path[1] holds Uri to mediaStore
			file = null;
			if (_path[0] != null) {
				file = new File(_path[0].getPath());
			} else {
				Cursor cursor = MediaStore.Images.Media.query(
						getContentResolver(), _path[1], null);
				cursor.moveToFirst();
				file = new File(cursor.getString(cursor
						.getColumnIndex(MediaStore.Images.Media.DATA)));
				cursor.close();
			}
			String filePath = file.getAbsolutePath();

			Drawable previewDrawable = Drawable.createFromPath(filePath);
			publishProgress(previewDrawable);
			ArrayList<String[]> items = new ArrayList<String[]>();
			try {
				GApp.exifDriver = null;
				GApp.exifDriver = ExifDriver.getInstance(filePath);
				items = ImageUtils.getImageMetas(GApp.exifDriver);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			items.add(0, new String[] { "Path", filePath });
			items.add(1, new String[] { "Size", (file.length()/1024)+"KB" });
			return items;
		}

		@Override
		protected void onProgressUpdate(Drawable... _preview) {
			ImageView imagePreview = (ImageView) findViewById(R.id.imagePreview);
			imagePreview.setImageDrawable(_preview[0]);
		}

		@Override
		protected void onPostExecute(ArrayList<String[]> items) {
			((AdapterExif) exifInfo.getAdapter()).setItems(items);
			progressDialog.dismiss();
		}
	}

	public void handleNext(View _view) {
		Intent intent = new Intent(this, ActivitySelectTarget.class);
		startActivity(intent);
	}

	public void handleBack(View _view) {
		Intent intent = new Intent(this, ActivityOptions.class);
		startActivity(intent);
	}

	public void handleProfile(View _view) {
		Intent intent = new Intent(this, ActivityProfile.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		startActivityForResult(intent, REQUEST_PROFILE);
	}

	public void handleInfo(View _view) {
		if (exifInfo.getVisibility() != View.GONE) {
			exifInfo.setVisibility(View.GONE);
		} else {
			exifInfo.setVisibility(View.VISIBLE);
		}
	}
}
